<%@ page import="com.complaint.rest.security.dto.ExampleUserDetails" %>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf8" language="java" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <title><spring:message code="label.complaints"/></title>
    <link rel="stylesheet" type="text/css" href="<c:url value="/resources/bootstrap/css/bootstrap.min.css"/>"/>
    <script src="<c:url value='/resources/bootstrap/js/bootstrap.min.js'/>"></script>
    <script src="<c:url value='/resources/scripts/jquery.min.js'/>"></script>
</head>
<body>
<%@include file="/WEB-INF/views/header.jsp" %>

<div class="panel panel-default">
    <div class="panel-heading">
        <h1><spring:message code="label.complaint.title"/></h1>
    </div>

    <div class="panel-body">
        <c:set var="authorId"
               value="<%=((ExampleUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId()%>"/>

        <form:form action="complaint" commandName="complaint" method="POST" enctype="utf8" role="form">
            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

            <c:if test="${!complaint.new}">
                <c:if test="${complaint.authorId ne authorId}">
                    <div class="form-group">
                        <label for="complaint-author" class="col-sm-2 control-label"><spring:message
                                code="label.complaint.author"/></label>

                        <div class="col-sm-10">
                            <div class="form-group">
                                <input id="complaint-author" value="${author}" class="form-control"/>
                            </div>
                        </div>
                    </div>
                </c:if>

                <div class="form-group">
                    <label for="complaint-publishDate" class="col-sm-2 control-label"><spring:message
                            code="label.complaint.publishDate"/></label>

                    <div class="col-sm-10">
                        <div class="form-group">
                            <form:errors id="error-publishDate" path="publishDate" for="complaint-publishDate"
                                         cssClass="control-label"/>
                            <fmt:formatDate value="${complaint.publishDate}" pattern="dd/MM/yyyy" var="publishDate"/>
                            <form:input id="complaint-publishDate" path="publishDate" value="${publishDate}"
                                        cssClass="form-control"
                                        placeholder="Publish Date"/>
                        </div>
                    </div>
                </div>
            </c:if>

            <div class="form-group">
                <label for="complaint-subject" class="col-sm-2 control-label"><spring:message
                        code="label.complaint.subject"/></label>

                <div class="col-sm-10">
                    <div class="form-group">
                        <form:errors id="error-subject" path="subject" for="complaint-subject"
                                     cssClass="control-label"/>
                        <form:input id="complaint-subject" path="subject" cssClass="form-control"
                                    placeholder="Subject"/>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="complaint-description" class="col-sm-2 control-label"><spring:message
                        code="label.complaint.description"/></label>

                <div class="col-sm-10">
                    <div class="form-group">
                        <form:errors id="error-description" path="description" for="complaint-description"
                                     cssClass="control-label"/>
                        <form:textarea id="complaint-description" path="description" cssClass="form-control" rows="5"
                                       placeholder="Description"/>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <div class="btn-group btn-group-sm">
                        <c:choose>
                            <c:when test="${complaint.new}">
                                <button type="submit" name="create" class="btn btn-default">
                                    <spring:message code="label.complaint.create"/>
                                </button>
                            </c:when>
                            <c:otherwise>
                                <c:if test="${complaint.authorId eq authorId}">
                                    <button type="submit" name="delete" class="btn btn-default"
                                            onclick="return confirm('Are you sure you want to delete?')">
                                        <spring:message code="label.complaint.delete"/>
                                    </button>
                                    <button type="submit" name="update" class="btn btn-default">
                                        <spring:message code="label.complaint.update"/>
                                    </button>
                                </c:if>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
            </div>
        </form:form>

        <a href="<c:url value="/index"/>">Back</a>
    </div>
</div>
</body>
</html>
